# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from MaoMiAV3.items import CandidSelfieItem


# 激情图区-自拍偷拍
class CandidselfieSpider(scrapy.Spider):
    name = 'CandidSelfie'
    custom_settings = {
        'ITEM_PIPELINES': {
            "MaoMiAV3.pipelines.CandidSelfiePipeline": 311
        }
    }
    allowed_domains = ['www.9f98eba932bbfaa6.pw']
    start_urls = ['https://www.9f98eba932bbfaa6.pw/tupian/list-自拍偷拍.html']

    def __init__(self):
        self.website_url = "https://www.9f98eba932bbfaa6.pw"
        self.settings_url = "https://www.9f98eba932bbfaa6.pw/tupian/list-自拍偷拍-{}.html"

    def parse(self, response):
        image_list = response.xpath("//div[@id='tpl-img-content']/li")
        for i in image_list:
            item = CandidSelfieItem()
            item["image_title"] = str(i.xpath("./a[1]/@title").extract()[0]).strip()
            image_url = i.xpath("./a[1]/@href").extract()[0]
            item["image_url"] = self.website_url + image_url
            yield item
        # 获取页数
        page_list = response.xpath("//div[@class='pagination']/a[last()-2]/text()").extract()[0]
        page_list = str(page_list).split("/")
        # 当前页数
        current_page = int(page_list[0])
        # 最后页数
        last_page = int(page_list[-1])
        # 判断当前页数是否小于或等于最后一页
        print("当前页数:", current_page)
        print("总页数:", last_page)
        if current_page < last_page:
            # 下一页
            next_page = current_page + 1
            next_page = self.settings_url.format(next_page)
            yield Request(url=next_page, callback=self.parse)
